package com.zhiyi.app.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.zhiyi.app.dao.ResourceMapper;
import com.zhiyi.app.dao.RoleMapper;
import com.zhiyi.app.dao.UserMapper;
import com.zhiyi.app.entity.User;
import com.zhiyi.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourceMapper resourceMapper;


    @Override
    public User login(User user) {
//        根据用户名获取用户信息
        User u= userMapper.getUserByName(user.getUsername());
//        比较密码是否一致
        if (u.getPassword().equals(SecureUtil.md5(user.getPassword()+"abc"))){
//            通过id获取用户角色
            List<String> role=roleMapper.getNameByUserId(u.getId());
            //            通过id获取用户权限
            List<String> resources=
                    resourceMapper.getResourceByUserId(u.getId());

            u.setRoles(role);
            u.setResouces(resources);
            return u;
        }
        return null;
    }
}
